package com.yunhe.sms.service.system.impl;

import com.yunhe.commons.dto.Pageable;
import com.yunhe.commons.util.StringUtils;
import com.yunhe.sms.dao.system.RoleDao;
import com.yunhe.sms.dao.system.impl.RoleDaoImpl;
import com.yunhe.sms.entity.system.Role;
import com.yunhe.sms.service.system.RoleService;

import java.util.List;

/**
 * Created by Administrator on 2017/10/23 0023.
 */
public class RoleServiceImpl implements RoleService{

    RoleDao roleDao = new RoleDaoImpl();

    @Override
    public void save(Role role,String permissionIds) {
        Long roleId = roleDao.save(role);
        if(null != roleId && StringUtils.notEmpty(permissionIds)){
            String[] permiIdArr = permissionIds.split(",");

            Object[][] param = new Object[permiIdArr.length][];
            for(int i = 0;i<permiIdArr.length;i++){
                param[i] = new Object[2];
                param[i][0] = roleId;
                param[i][1] = permiIdArr[i];
            }

            roleDao.saveRolePermissionPL(param);

        }
    }

    @Override
    public void delete(String ids) {
        if(null != ids){
            String[] idArr = ids.split(",");
            for(String i : idArr){
                Long id = Long.valueOf(i);
                roleDao.delete(id);
                roleDao.deleteByRole(id);
            }
        }
    }



    @Override
    public void update(Role role,String permissionIds) {
        roleDao.update(role);

        Long roleId = role.getId();
        roleDao.deleteByRole(roleId);

        if(null != roleId && StringUtils.notEmpty(permissionIds)){
            String[] permiIdArr = permissionIds.split(",");
            Object[][] param = new Object[permiIdArr.length][];
            for(int i = 0;i<permiIdArr.length;i++){
                param[i] = new Object[2];
                param[i][0] = roleId;
                param[i][1] = permiIdArr[i];
            }
            roleDao.saveRolePermissionPL(param);
        }


    }

    @Override
    public Role find(Long id) {
        Role role = roleDao.find(id);
        return role;

    }

    @Override
    public List<Role> findAll() {
        List<Role> roles = roleDao.findAll();
        return roles;
    }

    @Override
    public List<Role> findAllUsable() {
        List<Role> roleList = roleDao.findAllUsable();
        return roleList;
    }

    @Override
    public Pageable<Role> findPage(Integer offset, Integer size) {
        Pageable<Role> pageable = roleDao.findPage(offset, size);
        return pageable;
    }
}
